import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../pages/HomeView.vue'
import index from '../pages/index/index.vue'
import login from '../pages/login/index.vue'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
import store from '@/store'
const routes = [
  {
    path: '/',
    name: 'index',
    component: index,
    redirect: '/homeView',
    children: [
      {
        path: '/homeView',
        name: 'homeView',
        component: () => import('../pages/index/homeView.vue')
      },
      {
        path: '/category',
        name: 'category',
        component: () => import('../pages/communal/category.vue')
      },
      {
        path: '/product',
        name: 'product',
        component: () => import('../pages/communal/product.vue')
      },
      {
        path: '/shopCar',
        name: 'shopCar',
        component: () => import('../pages/communal/shopCar.vue')
      }
    ]
  },

  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/category',
    name: 'category',
    component: () => import('../pages/communal/category.vue'),
  },
  {
    path: '/shopCar',
    name: 'shopCar',
    component: () => import('../components/HelloWorld.vue'),
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../pages/AboutView.vue'),
  },
  {
    path: '/swiper',
    name: 'swiper',
    component: () => import('../pages/HomeView.vue'),
  },

]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, form, next) => {
  if (to.path == '/shopCar') {
    console.log(store);
    if (store.state.userName.token) {
      next()
    } else {
      next('/')
    }

  } else {
    next()
  }
  if (form.path == '/category' && to.path == '/homeView') {
    to.meta.keepAlive = true
    console.log('设置缓存');
    // store.commit('getisRouter', 'homeView')
  } else if (form.path == '/homeView' && to.path == '/category') {
    console.log('不进行操作');
  }
  else {
    to.meta.keepAlive = false
    form.meta.keepAlive = false
    console.log('取消缓存');
    // store.commit('getisRouter', '')
  }

})

export default router
